package com.efounder.frame.manager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;

import com.efounder.mobilemanager.R;
import com.efounder.utils.ImageSpliceUtil;
import com.efounder.utils.ResStringUtil;
import com.utilcode.util.ImageUtils;
import com.utilcode.util.LogUtils;
import com.utilcode.util.ReflectUtils;
import com.utilcode.util.ThreadManager;

import java.io.File;

/**
 * 截屏分享管理
 *
 * @author yqs
 * @date 2018/11/6
 */
public class ScreenShotShareManager {

    private static final String TAG = "ScreenShotListenManager";

    private Context context;
    private ReflectUtils reflectUtils;
    private ProgressDialog progressDialog;

    public ScreenShotShareManager(Context context) {
        this.context = context;
        progressDialog = new ProgressDialog(context);
        try {
            reflectUtils = ReflectUtils.reflect(ResStringUtil.getString(com.efounder.mobilemanager.R.string.manager_share_screen_shot_class));
            reflectUtils = reflectUtils.newInstance();
        } catch (Exception e) {
            LogUtils.e("com.efounder.UShareScreenShotHelper未找到,不初始化截图功能");
        }
    }

    /**
     * 分享图片
     *
     * @param imagePath 截图路径
     */
    public void shareSceenShotPic(final String imagePath) {
        progressDialog.setMessage(ResStringUtil.getString(R.string.common_text_loading));
        progressDialog.show();
        ThreadManager.getThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //拼接图片
                Bitmap bitmap = ImageSpliceUtil.mergeBitmap_TB(BitmapFactory.decodeFile(imagePath),
                        BitmapFactory.decodeResource(context.getResources(), R.drawable.share_botton_join), true);
                //文件保存路径
                final String fielPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString()
                        + File.separator
                        + System.currentTimeMillis() + ".png";
                //保存拼接图片
                ImageUtils.save(bitmap, fielPath, Bitmap.CompressFormat.PNG, true);

                ((Activity) context).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!((Activity) context).isDestroyed()) {
                            progressDialog.dismiss();
                            //调用友盟分享
                            try {
                                reflectUtils.newInstance().method("share", context, fielPath,
                                        imagePath, R.drawable.ic_launcher, "", null);
                                // 通知图库更新
                                ImageSpliceUtil.galleryAddPic(fielPath);
//                                //删除原图
//                                new File(imagePath).delete();
//                                ImageSpliceUtil.deletePic(imagePath);
                            } catch (Exception e) {
                                LogUtils.d("没有找到分享的类，分享原图");
                                new File(fielPath).delete();
                                shareToFriend(imagePath);
                            }
                        }
                    }
                });
            }
        });
    }

    /**
     * 释放友盟分享
     */
    public void releaseUshare() {
        //释放分享，避免内存泄漏
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        try {
            reflectUtils.method("release", context);
        } catch (Exception e) {
            LogUtils.e("reflectUtils-release未找到");
        }
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        try {
            reflectUtils.method("onActivityResult", context, requestCode, resultCode, data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void shareToFriend(String imagePath) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("image/*");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            String className = "com.efounder.chat.activity.MobileShareActivity";
            Class clazz = Class.forName(className);
            intent.setClass(context, clazz);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}